<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 方式1 -栅格布局 -->
      <!-- <el-row type="flex" class="row-bg">
          <el-col :span="12">左</el-col>
          <el-col :span="12">右</el-col>
        </el-row> -->
      <!-- 方式2  - flex布局 (推荐)-->
      <!-- <el-row>
          <el-col type="flex">
            <el-col :span="12">左</el-col>
            <el-col :span="12">右</el-col>
          </el-col>
        </el-row> -->
      <el-card class="tree-card">
        <TreeTool :data="company"></TreeTool>

        <el-tree v-loading="isLoading" :data="list" :props="{ label: 'name' }" default-expand-all="">
          <!-- 作用域插槽 -->
          <!-- 这里的data就是每一行的数据 -->
          <template v-slot="{ data }">
            <tree-tool :data="data">
              <el-dropdown-item
                @click.native="onAdd(data)"
              >添加子部门</el-dropdown-item>
              <el-dropdown-item
                @click.native="onEdit(data)"
              >编辑子部门</el-dropdown-item>
              <el-dropdown-item
                @click.native="onDel(data.id)"
              >删除子部门</el-dropdown-item>
            </tree-tool>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- .sync修饰符,简化父组件的模板,只要传数据 -->
    <!-- <add-dept :visible="showDialog" @update:visible="showDialog=$event"></add-dept> -->
    <add-dept
      ref="addDeptRef"
      :visible.sync="showDialog"
      :node="currentNode"
      @success="getList()"
    ></add-dept>
  </div>
</template>

<script>
import TreeTool from './components/tree-tool.vue'

import { getList, delDept, getDeptById } from '../../api/departments.js'
import { translateListToTree } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTool,
    AddDept
  },
  props: {},
  data() {
    return {
      // 定义数据
      isLoading: false,
      showDialog: false, // 对话框的显示
      currentNode: {}, // 当前操作的部门
      company: { name: '{{this.company.name}}', manager: '负责人' },
      list: []
    }
  },
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    async getList() {
      this.isLoading = true
      const { companyName, depts } = await getList()
      this.company.name = companyName
      this.list = depts
      this.list = translateListToTree(depts, '')
      this.isLoading = false
    },
    // 添加
    onAdd(node) {
      this.currentNode = node
      this.showDialog = true
    },
    // 修改
    async onEdit(node) {
      console.log('修改')
      this.currentNode = node
      this.$refs.addDeptRef.form = await getDeptById(node.id)
      this.showDialog = true
    },
    // 删除
    onDel(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        // 发请求
        await delDept(id)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新
        this.getList()
        console.log('删除')
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
